from django.shortcuts import render, get_object_or_404
from teacher.models import Teacher, TeacherHistory
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .forms import EditorForm
from django.contrib.auth.decorators import login_required
from . import func
import mistune

# Create your views here.

def index(request):
	all_teacher = Teacher.objects.all()
	context = {"tea_t":all_teacher}
	return render(request, "teacher/index.html", context)

@login_required
def look(request, teacher_id):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	context = {"look_teacher":look_teacher}
	render_md = mistune.create_markdown(plugins=['table', 'strikethrough'])
	markdown_html = render_md(look_teacher.teacher_intro)
	# markdown_html = func.filler_replace(markdown_html)
	context['markdown'] = markdown_html
	return render(request, "teacher/look.html", context)

@login_required
def history(request, teacher_id):
	look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
	all_history = look_teacher.history.all().order_by('-last_edit_date')
	context = {'look_teacher': look_teacher, 'all_history': all_history}
	return render(request, 'teacher/history.html', context=context)

@login_required
def history_spe(request, teacher_id, serial_number):
	now_history = get_object_or_404(TeacherHistory, id=serial_number)
	render_md = mistune.create_markdown(plugins=['strikethrough', 'table'])
	markdown_html = render_md(now_history.teacher_intro)
	context = {'now_history': now_history, 'markdown_html': markdown_html, 'teacher_id': teacher_id}
	return render(request, 'teacher/history_spe.html', context=context)

@login_required
def editor(request):
	if request.method == 'POST':
		editor_form = EditorForm()
		new_teacher = Teacher()
		if request.POST["intro"] == None or request.POST["subject"] == None or request.POST["name"] == None:
			return render(request, "teacher/editor.html", context={'editor_form': editor_form, 'errmsg': '内容不得为空！'})
		if request.POST['last_year'] == None or request.POST['host_year'] == None:
			return render(request, "teacher/editor.html", context={'editor_form': editor_form, 'errmsg': '内容不得为空！'})

		new_teacher.teacher_intro = request.POST['intro']
		new_teacher.last_year = request.POST['last_year']
		new_teacher.host_year = request.POST['host_year']
		new_teacher.subject = request.POST['subject']
		new_teacher.name = request.POST["name"]
		new_teacher.last_edit_name = request.user.username

		count = 0
		all_teacher = Teacher.objects.all()

		for i in all_teacher:
			count += 1

		new_teacher.teacher_id = count + 1
		new_teacher.save()

		return HttpResponseRedirect(reverse("teacher:look", kwargs={"teacher_id":new_teacher.teacher_id}))
	else:
		editor_form = EditorForm()
		return render(request, "teacher/editor.html", context={'editor_form':editor_form})

@login_required
def editor_old(request, teacher_id):
	if request.method == 'POST':
		look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
		if request.POST["intro"] == None or request.POST["subject"] == None or request.POST["name"] == None:
			return render(request, "teacher/error_null.html")
		if request.POST['last_year'] == None or request.POST['host_year'] == None:
			return render(request, 'teacher/error_null.html')

		new_history = TeacherHistory()
		new_history.teacher_intro = look_teacher.teacher_intro
		new_history.last_year = look_teacher.last_year
		new_history.host_year = look_teacher.host_year
		new_history.subject = look_teacher.subject
		new_history.name = look_teacher.name
		new_history.last_edit_name = look_teacher.last_edit_name
		new_history.last_edit_date = look_teacher.last_edit
		new_history.history = look_teacher
		new_history.save()

		look_teacher.teacher_intro = request.POST['intro']
		look_teacher.last_year = request.POST['last_year']
		look_teacher.host_year = request.POST['host_year']
		look_teacher.subject = request.POST['subject']
		look_teacher.name = request.POST["name"]
		look_teacher.last_edit_name = request.user.username
		look_teacher.save()

		return HttpResponseRedirect(reverse("teacher:look", kwargs={"teacher_id":look_teacher.teacher_id}))
	else:
		look_teacher = get_object_or_404(Teacher, teacher_id=teacher_id)
		editor_form = EditorForm()
		editor_form.fields['name'].initial = look_teacher.name
		editor_form.fields['last_year'].initial = look_teacher.last_year
		editor_form.fields['host_year'].initial = look_teacher.host_year
		editor_form.fields['intro'].initial = look_teacher.teacher_intro
		editor_form.fields['subject'].initial = look_teacher.subject
		context = {"look_teacher":look_teacher, 'editor_form':editor_form}
		return render(request, "teacher/editor_old.html", context=context)
